package com.example.elemboot.Controller;

import com.example.elemboot.Dao.IDeliveryAddress;
import com.example.elemboot.Dao.IUser;
import com.example.elemboot.Entity.DeliveryAddress;
import com.example.elemboot.Entity.User;
import com.example.elemboot.Utils.Result;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/deliveryAddress")
public class DeliveryAddressController {

    @Resource
    private IDeliveryAddress deliveryAddressDao;

    @Resource
    private IUser userDao;

    //根据用户id获取地址
    @GetMapping("/getAddressByUserId/{userPhone}")
    public Result getAddressByUserId(@PathVariable("userPhone") String userPhone){
        User user = userDao.findUserByUserPhone(userPhone);
        List<DeliveryAddress> addresses = deliveryAddressDao.findDeliveryAddressesByUser(user);
        return Result.success(addresses);
    }

    //根据地址id获取地址
    @GetMapping("/getDeliveryAddressById/{addressId}")
    public Result getDeliveryAddressById(@PathVariable("addressId") int addressId){
        DeliveryAddress deliveryAddress = deliveryAddressDao.findById(addressId).orElse(null);
        return Result.success(deliveryAddress);}

    //添加地址
    @PostMapping("/saveDeliveryAddress")
    public  Result saveDeliveryAddress(@RequestParam("contactName") String contactName,
                                       @RequestParam("contactSex") Integer contactSex,
                                       @RequestParam("contactTel") String contactTel,
                                       @RequestParam("address") String address,
                                       @RequestParam("userPhone") String userPhone
                                       ){
        User user = userDao.findUserByUserPhone(userPhone);
        DeliveryAddress deliveryAddress = new DeliveryAddress(contactName, contactSex, contactTel, address,user);
        deliveryAddressDao.save(deliveryAddress);
        return Result.success("添加成功");
    }

//    @PostMapping("/saveDeliveryAddress")
//    public  Result saveDeliveryAddress(@RequestBody DeliveryAddress deliveryAddress
//    ){
//        deliveryAddressDao.save(deliveryAddress);
//        return Result.success("添加成功");
//    }

    //更新地址
    @PostMapping("/updateDeliveryAddress")
    public Result updateDeliveryAddress(@RequestParam("contactName") String contactName,
                                         @RequestParam("contactSex") Integer contactSex,
                                         @RequestParam("contactTel") String contactTel,
                                         @RequestParam("address") String address,
                                         @RequestParam("daId") Integer daId){

        DeliveryAddress address_DB = deliveryAddressDao.findById(daId).get();
        DeliveryAddress deliveryAddress = new DeliveryAddress(daId, contactName, contactSex, contactTel, address,address_DB.getUser());
        BeanUtils.copyProperties(deliveryAddress,address_DB);
        deliveryAddressDao.save(address_DB);
        return Result.success("修改成功");
    }
//    @PostMapping("/updateDeliveryAddress")
//    public Result updateDeliveryAddress(@RequestBody DeliveryAddress deliveryAddress){
//        DeliveryAddress address_DB = deliveryAddressDao.findById(deliveryAddress.getDaId()).get();
//        BeanUtils.copyProperties(deliveryAddress,address_DB);
//        deliveryAddressDao.save(address_DB);
//        return Result.success("修改成功");
//    }

    //删除
    @GetMapping("/removeDeliveryAddress/{addressId}")
    public Result removeDeliveryAddress(@PathVariable("addressId") int addressId){
        deliveryAddressDao.deleteById(addressId);
        return Result.success("删除成功");
    }


}
